<template>
  <div class="outside">
    <div class="top">
      <div class="left">
        <img :src="profile.profile.avatarUrl" alt="">
      </div>
      <div class="right">
        <div class="nickname">{{profile.profile.nickname}}</div>
        <div class="genlev">
          <span> Lv.{{profile.level}}</span>
          <img v-show="profile.profile.gender===1" src="~assets/img/male.png" alt="">
          <img v-show="profile.profile.gender===2" src="~assets/img/female.png" alt="">
          <div v-show="profile.profile.userId !== MyId" @click="changeFollow">
            <p v-show="!isfollow"><i class='iconfont icon-jiahao' style="color:var(--netease-color);margin-right:5px"></i>关注</p>
            <p v-show="isfollow"><i class='iconfont icon-duihao' style="margin-right:5px"></i>已关注</p>
          </div>
          <div @click="goEditPro" v-show="profile.profile.userId === MyId" >编辑信息</div>
        </div>  
        <div class="dgf">
          <div style="font-size:18px;color:#000">
            <span @click="dongTai">{{profile.profile.eventCount}}</span>
            <span @click="guanZhu">{{profile.profile.follows}}</span>
            <span @click="fenSi">{{profile.profile.followeds}}</span>
          </div>
          <div><span @click="dongTai">动态</span><span @click="guanZhu">关注</span><span @click="fenSi">粉丝</span></div>
        </div>
        <div class="another">所在地区：<span>{{profile.profile.province | ToCode(province)}}</span></div>
        <div class="another">个人介绍：<span>{{profile.profile.signature ? profile.profile.signature : '暂无介绍'}}</span></div>
      </div>
    </div>

    <div>
      <rec-playlist :title="'歌单'" :List="playlist" :num=6></rec-playlist>
      <rec-playlist :title="'收藏'" :List="collection" :num=6></rec-playlist>
    </div>
  </div>
</template>

<script>
import {request} from 'network/request'
import {mapState} from 'vuex'
import {province} from './province'
import recPlaylist from '../discover/childroute/recommend/childComps/recPlaylist.vue'
export default {
  name:"Profile",
  components:{
    recPlaylist
  },
  data(){
    return {
      profile:{},
      playlist:[],
      province,
      hasMore:false,
      collection:[],
      isfollow:false
    }
  },
  computed:{
    ...mapState({
      MyId:state=>state.profile.profile.userId,
    })
  },
  methods:{
    goEditPro(){
      this.$router.push('/editprofile')
    },
    async changeFollow(){
      var data = null
      if(this.isfollow){
        data = await request("GET",'follow',{id:this.$route.params.id,t:0})
      }else{
        data = await request("GET",'follow',{id:this.$route.params.id,t:1})
      }
      if(data.code === 200){
        this.isfollow = !this.isfollow
      }
    },
    dongTai(){
      this.$router.push('/dongtai/' + this.$route.params.id + `/${this.profile.profile.nickname}`)
    },
    guanZhu(){
      this.$router.push(`/follows/${this.$route.params.id}/${this.profile.profile.nickname}/` + 0)
    },
    fenSi(){
      this.$router.push(`/follows/${this.$route.params.id}/${this.profile.profile.nickname}/` + 1)
    }
  },
  async created(){
    const data = await request("GET",'user/detail',{uid:this.$route.params.id})
    const data2  = await request("GET",'user/playlist',{uid:this.$route.params.id})
    this.profile = data
    this.isfollow = data.profile.followed
    this.playlist = data2.playlist.filter(item=>item.ordered === false)
    this.collection = data2.playlist.filter(item=>item.ordered === true)
    this.hasMore = data2.more
    // console.log(data)

  },
  filters:{
    ToCode(num,provinces){
      let region = null
      for(var item of provinces){
        if(Object.keys(item)*1 === num*1){
          region = Object.values(item).toString()
          return region
        }else{ region = '海外'}
      }
      return region
      // let region = provinces.filter((currentValue)=>{
      //   Object.keys(currentValue)*1 === num*1})
      // console.log(region)
      // return region
    }
  }
  
}
</script>

<style scoped>
  .outside{
    padding-bottom: 30px;
  }
  .top{
    display:flex;
    padding-top:20px;
    height: 200px;
  }
  .left img{
    border-radius: 50%;
    width: 185px;
    height: 185px;
    border:1px solid rgb(201,195,191)
  }
  .right{
    padding-left:20px;
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  .nickname{
    font-weight: 800;
    font-size: 22px;
    color: var(--font-color);
  }
  .genlev{
    position: relative;
    display: flex;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid rgb(229,229,229);
  }
  .genlev span{
    font-size: 8px;
    padding: 2px 8px;
    background-color: rgb(240,240,240);
    border-radius: 30px;
  }
  .genlev img{
    width: 18px;
    height: 18px;
    margin-left: 5px;
  }
  .genlev div{
    position:absolute;
    right:0;
    text-align: center;
    width: 80px;
    height: 30px;
    line-height: 30px;
    background-color:#fff;
    border-radius: 30px;
    margin:0 10px;
    color:rgb(55,55,55);
    cursor:pointer;
    border:1px solid rgb(216,216,216);
    font-size: 14px;
  }
  .genlev div:hover{
    background-color: rgb(242,242,242);
  }
  .dgf{
    padding-top: 10px;
    height:50px;
    width: 20%;
    /* border-bottom: 1px solid rgb(237,237,237); */
    font-size: 12px;
    color: rgb(102,102,102);
  }
  .dgf div{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .dgf span{
    cursor: pointer;
    width: 25px;
  }
  .another{
    font-size: 12px;
    padding-bottom: 5px;
  }
  .another span{
    color:rgb(103,103,103)
  }
</style>